package mn.idax.exchange.otc.service;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ChatRedisService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ChatRedisService.class);

    private final static String OTC_ORDER_USER_KEY = "OTC:ORDER:USER";
    private final static String OTC_CHAT_FILE = "OTC:CHAT:FILE";
    private final static String OTC_CHAT_MESSAGE = "OTC:CHAT:MESSAGE";

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public String getValueFromOtcOrderUser(String orderId) {
        try {
            if (stringRedisTemplate.hasKey(OTC_ORDER_USER_KEY)) {
                BoundHashOperations<String, String, String> hashOperations = stringRedisTemplate.boundHashOps(OTC_ORDER_USER_KEY);
                return hashOperations.get(orderId);
            }
        } catch (Exception e) {
            LOGGER.error("----get value from otc_order_user error, orderId:{}----", orderId, e);
        }
        return null;
    }

    public boolean setValueToOtcOrderUser(String orderId, String user) {
        try {
            BoundHashOperations<String, String, String> hashOperations = stringRedisTemplate.boundHashOps(OTC_ORDER_USER_KEY);
            hashOperations.putIfAbsent(orderId, user);
            return true;
        } catch (Exception e) {
            LOGGER.error("----set value to otc_order_user error, orderId:{}, user:{}----", orderId, user, e);
        }
        return false;
    }

    public Object getChatFileInfo(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        try {
            BoundHashOperations operations = stringRedisTemplate.boundHashOps(OTC_CHAT_FILE);
            return operations.get(key);
        } catch (Exception e) {
            return null;
        }
    }
    
    public Long deleteChatFileInfo(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        try {
            BoundHashOperations operations = stringRedisTemplate.boundHashOps(OTC_CHAT_FILE);
            return operations.delete(key);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * pushMessageToRedis 发送消息到redis,服务端订阅后再进行通知
     *
     * @param topic
     * @param message
     */
    public void pushMessageToRedis(String topic, String message) {
        stringRedisTemplate.convertAndSend(topic, message);
    }

    /**
     *
     * 功能描述:
     *      后续考虑加入过期时间，避免占用磁盘空间过多
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/10/11 11:44
     */
    public boolean verifyReplaceMessage(String messageId){
        if (StringUtils.isEmpty(messageId)){
            return false;
        }
        return !stringRedisTemplate.boundHashOps(OTC_CHAT_MESSAGE).putIfAbsent(messageId, messageId);
    }
}
